package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.Frame.RoleSkillJframe;
import org.come.bean.Skill;
import org.come.until.AnalysisString;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.ScrollUI;
import org.come.until.UserData;

import com.tool.btn.FormsOnOffBtn;
import com.tool.image.ImageMixDeal;
import com.updateNew.MyIsif;

public class RoleSkillJpanel extends JPanel  {

	private RoleSkillJframe roleSkillJframe;

	
	private JScrollPane jScrollPane1,// 放置法术列表的滚动条
	jScrollPane2;// 放置法术说明的滚动条

    private JList<String> listspell,// 放置法术列表
    listspellctro;// 放置法术说明列表
    
	private DefaultListModel<String> modelspell,// 放置法术列表对象
	modelspellctro;// 放置法术说明对象
	
	/** 存放技能*/
	public List<Skill> skills=new ArrayList<>();
	private  StringBuilder builder;//存放法术说明文件 调用时需要 builder.String();
	private JLabel skilljLabel;//存放法术说明载体;skilljLabel.setext(builder.String());
	final ImageIcon imageIcon = new ImageIcon("img/123_副本.png");
	
	public static JLabel labgundong;
	
	public RoleSkillJpanel(RoleSkillJframe roleSkillJframe) throws Exception {
    		labgundong = new JLabel(new ImageIcon("skin/8100226/button/23.png"));
		
		
		builder=new StringBuilder();
		this.roleSkillJframe = roleSkillJframe;
		this.setPreferredSize(new Dimension(441, 395));
		this.setLayout(null);
		this.setBackground(Color.BLACK);

		//关闭按钮事件
		FormsOnOffBtn offBtn=new FormsOnOffBtn("inkImg/hongmu/2/8_jpg.button.btn_close.jpg", 1,9);
		offBtn.setBounds(421, 0, 23, 23);
		this.add(offBtn);
		
		//角色法术列表对象
		modelspell = new DefaultListModel<String>();


			labgundong.setBounds(185, 81, 17, 271);
			this.add(labgundong);


		//角色法术列表
		listspell= new JList<String>()
				{  
		      Image image = imageIcon.getImage();  
		      
		      Image grayImage = GrayFilter.createDisabledImage(image);  
		      {  
		        setOpaque(false);  
		      } // instance initializer  
		  
		  
		      @Override
			public void paint(Graphics g) {  
		    	  
		    	
		  				// 背景图片
		  				g.drawImage(grayImage, 0,0, this);
		  			
		        super.paint(g);  
		      }  
		    };
				
		listspell.setSelectionBackground(new Color(33,42,52));
		listspell.setSelectionForeground(Color.WHITE);
		listspell.setForeground(Color.WHITE);
		listspell.setFont(new Font("宋体",   1,   16));
		listspell.setOpaque(false);
		listspell.setBackground(new Color(0,0,0,0)); //设置列表框为透明背景
		listspell.setModel(modelspell);
		listspell.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				 int tmp = 0;

				 String stmp ="";
				 int[] index = listspell.getSelectedIndices();

				         //index值，这些index值由一个int array返回.
				 if(index.length>0){
					 
				 for (int i = 0; i < index.length; i++) {
				 tmp = index[i];
				 
				 }
				 
				 Skilltest(tmp);
				 }
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				//打开了窗体
				if(e.isMetaDown()){//检测鼠标右键单击
				           FormsManagement.HideForm(9);
						}
				
			}
		});
		//角色法术列表滚动条
		jScrollPane1 = new JScrollPane(listspell);
		jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		jScrollPane1.getVerticalScrollBar().setUI(new ScrollUI());
		jScrollPane1.getViewport().setOpaque(false);
		jScrollPane1.setOpaque(false);
		jScrollPane1.setBounds(25, 62, 177, 308);
		jScrollPane1.setBorder(BorderFactory.createLineBorder(new Color(31, 7, 8)));
		jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		this.add(jScrollPane1);
		String gdt = "";
        	gdt = "skin/8100226/button/23.png";
		//法术说明列表对象
				modelspellctro = new DefaultListModel<String>();

				skilljLabel=new JLabel();
				skilljLabel.setBounds(240,10,150,271);
				this.add(skilljLabel);
				if(modelspellctro.size()<=14){
//					JLabel labgundong = new JLabel(new ImageIcon("inkImg/hongmu/2/gundongtiao_副本.png"));
					JLabel labgundong = new JLabel(new ImageIcon(gdt));
					labgundong.setBounds(393,80,17, 271);
					this.add(labgundong);
					
				}

				//法术说明列表
				listspellctro= new JList<String>()
						{  
				      Image image = imageIcon.getImage();  
				      
				      Image grayImage = GrayFilter.createDisabledImage(image);  
				      {  
				        setOpaque(false);  
				      } // instance initializer  
				  
				  
				      @Override
					public void paint(Graphics g) {  
				    	  
				    	
				  				// 背景图片
				  				g.drawImage(grayImage, 0,0, this);
				  			
				        super.paint(g);  
				      }  
				    };
						
				listspellctro.setSelectionBackground(new Color(33,42,52));
				listspellctro.setSelectionForeground(Color.YELLOW);
				listspellctro.setForeground(Color.YELLOW);
				listspellctro.setFont(new  java.awt.Font("宋体",   0,   14));
				listspellctro.setOpaque(false);
				listspellctro.setBackground(new Color(0,0,0,0)); //设置列表框为透明背景
				listspellctro.setModel(modelspellctro);
				listspellctro.addMouseListener(new MouseListener() {
					
					@Override
					public void mouseReleased(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mousePressed(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseExited(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseEntered(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseClicked(MouseEvent e) {
						//打开了窗体
						if(e.isMetaDown()){//检测鼠标右键单击
						           FormsManagement.HideForm(9);
								}
						
					}
				});
				
				//法术说明列表滚动条
				jScrollPane2 = new JScrollPane(listspellctro);
				jScrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				jScrollPane2.getVerticalScrollBar().setUI(new ScrollUI());
				jScrollPane2.getViewport().setOpaque(false);
				jScrollPane2.setOpaque(false);
				jScrollPane2.setBounds(238, 61, 172, 308);
				jScrollPane2.setBorder(BorderFactory.createLineBorder(new Color(31, 7, 8)));
				jScrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				this.add(jScrollPane2);

	}
    //背景
	private ImageIcon icon;
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		// 技能
		if (icon==null) {
            icon=new ImageIcon("inkImg/hongmu/2/91_png.xy2uiimg.skill.png");
        }
    	
		g.drawImage(icon.getImage(), 0,
				0, 441, 395, this);

		if(modelspell.size()>=15){

		this.remove(labgundong);
		
	}
		
	}
	
	/**
	 * 添加技能
	 */
	public void addskill(String v,int i){
//		String[] vs=v.split("=");
//		 skills.add(UserMessUntil.getskill(vs[0]));
//	     skills.get(i).setSkilled((int)Double.parseDouble(vs[1]));
//		 modelspell.add(i,vs[0]+"  "+vs[1]);		
	}
	/**
	 * 创建法术说明文本
	 */
	public void Skilltest(int i){
		 builder.delete(0, builder.length());
		 builder.append("<html>");
		 builder.append(StringReplace(skills.get(i),AnalysisString.lvlint(ImageMixDeal.userimg.getRoleShow().getGrade())));
		 builder.append("</html>"); 
		 skilljLabel.setText(builder.toString());
	}
	/**
	   * 字符串替换
	   */
	  public static String StringReplace(Skill RoleSkill,int renlvl){
		  Pattern p = Pattern.compile("】(.*?)<");
		  Matcher m = p.matcher(RoleSkill.getRemark());
		  Pattern pend=Pattern.compile("</font></p><p><font color='#FFFFFF'>(.*?)</font></p>");
		  Matcher mend = pend.matcher(RoleSkill.getRemark());
		  String  end = null;
		  String[]  CharReplace=new String[4];
		   while (mend.find()) {
			end="</font></p><p><font color='#FFFFFF'>"+mend.group(1)+"</font></p>";
		}
		  StringBuffer sb = new StringBuffer();
		  int i=0;
		  int j=0;
		  while (m.find()) {
			  if (i==6) {
				  m.appendReplacement(sb,"】"+ CharReplace[j]+end);
			}else
			  if (i>2) {  
				  m.appendReplacement(sb,"】"+ CharReplace[j]+"<");
				  j++;	  
			  }else if (i==2) {
				 CharReplace=Analysis(m.group(1), RoleSkill, renlvl);
			   }
			 
			  i++;		  
			}		
		  return sb.toString();
	  }
	  
	 
	  
	  
	  /**
	   *  根据法术类型来算值
	   */
	  public static String[] Analysis(String type,Skill skill,int renlvl){
		  String[]  CharReplace=new String[4];
		  CharReplace[0]=geshu(skill,type);

//		  技能等级
		 double lvl =Double.parseDouble(skill.getSkilllevel());
//		  技能熟练度
		 double sld=skill.getSkilled();
//		  技能成长 
		 double sv=Double.parseDouble(skill.getGrow());
//		 耗蓝介质
		 double mv=Double.parseDouble(skill.getDielectric());
//		  介值
		 double value=Double.parseDouble(skill.getValue());
		  
		  if ("混乱".equals(type)|| "封印".equals(type)|| "昏睡".equals(type)|| "遗忘系魔法".equals(type)) {
			
	          //机率
			  CharReplace[1]=(int)(value+sv*new BigDecimal(Math.pow(sld, 0.3)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue())+"%";
			
			  //回合数
			  CharReplace[2]="7";
			  
			  //耗蓝	
			  CharReplace[3]=(int)(mv*((sld/25000)+1))+"";										  
		 
		 }else if ("中毒".equals(type)) {
			  //回合数
			  CharReplace[1]="5";
			  //伤害
			  CharReplace[2]=UserData.xiaoshu3((double)((int)((value+sld*sv/1000)*1000))/15)+"%";	
//			  中毒的概率是伤害*12
			  //耗蓝	
			  CharReplace[3]=(int)(mv*((sld/25000)+1))+"";		
			 
	 	 }else if ("震慑".equals(type)) {
	 		 //伤害
			  CharReplace[1]=(double)((int)((value+sld*sv/1000)*1000))/10+"";	
			  //耗蓝	
			  CharReplace[2]=(int)(mv*((sld/25000)+1))+"";		
	 		 
	 		 
	 	 }else if ("力量增益".equals(type)|| "抗性增益".equals(type)|| "加速增益".equals(type)) {
	 		
	 	        //  增益百分比
	 			  CharReplace[1]=(double)((int)((value+sld*sv/1000)*1000))/10+"";
	 			
	 			  //回合数
	 			  CharReplace[2]="7";
	 			  
	 			  //耗蓝	
	 			  CharReplace[3]=(int)(mv*((sld/25000)+1))+"";	
	 		 
	 	 }else if ("风系魔法".equals(type)|| "雷系魔法".equals(type)|| "水系魔法".equals(type)|| "火系魔法".equals(type)) {
	  		
	     	  //伤害
			  CharReplace[1]=(int)((value+sv*(1+5*sld/5000*(10-sld/5000)/2))*renlvl)+"";
			  //耗蓝	
			  CharReplace[2]=(int)(mv*((sld/25000)+1))+"";
	  	 }else if ("鬼火系魔法".equals(type)) {
//	  		 每己方死了一个增加10%的伤害 
	  		 //伤害
			  CharReplace[1]=(int)((value+sv*(1+5*sld/5000*(10-sld/5000)/2))*renlvl)+"";
			  //耗蓝	
			  CharReplace[2]=(int)(mv*((sld/25000)+1))+"";
	    	 }else if ("三尸虫".equals(type)) {
	    		 // 伤害
				  CharReplace[1]=(double)((int)((value*renlvl+sld*sv/1000)*1000))/10+"回血程度"+(value*100+(int)(sld/250))+"%";
				
				  //耗蓝	
				  CharReplace[2]=(int)(mv*((sld/25000)+1))+"";	
	    		 
	    		 
	    	 }else if ("遗忘系魔法".equals(type)) {
//	    		 中了魔法 如果该回合还未出手切不处于防御状态  设为进行普攻状态
	    	  //伤害
	   		  CharReplace[1]=(int)(value*renlvl+sld*sv/1000)+""; 
	   		  //耗蓝	
	   		  CharReplace[2]=(int)(mv*((sld/25000)+1))+"";	 
	     	 }
	     return CharReplace;
	  }
	  

	  /**
	   * 根据熟练度计算个数
	 * @param type 
	   */
	  public static String geshu(Skill skill, String type){	
		  int lvl=Integer.parseInt(skill.getSkilllevel());
		  if ("鬼火系魔法".equals(type)|| "火系魔法".equals(type)|| "水系魔法".equals(type)|| "雷系魔法".equals(type)|| "风系魔法".equals(type)) {
			  return xian(skill.getSkilled(), lvl)+"";
		  }else
		  if ("震慑".equals(type)) {
			  return moz(skill.getSkilled(), lvl)+"";
		  }else
		  if ("力量增益".equals(type)|| "抗性增益".equals(type)|| "加速增益".equals(type)) {
			  return  moq(skill.getSkilled(), lvl)+"";
		  }else
		  if ("中毒".equals(type)|| "封印".equals(type)|| "昏睡".equals(type)|| "遗忘系魔法".equals(type)) {
				  return  renq(skill.getSkilled(), lvl)+"";
		  }else
		  if ("混乱".equals(type)) {
				  return  renh(skill.getSkilled(), lvl)+"";
		  }else
		  if ("混乱".equals(type)) {
				  return  renh(skill.getSkilled(), lvl)+"";
		  }else
		  if ("三尸虫".equals(type)) {
				  return  guis(skill.getSkilled(), lvl)+"";
		  }else {
			  return xian(skill.getSkilled(), lvl)+"";
		  }
	  }
	/**
	 * 仙法个数
	 */
	  public static int xian(int ed,int lvl){
		   if (lvl==3) {
			   if (ed<720) {
					return 2;
				}else if (ed<5215) {
					return 3;
				}else if (ed<16610){
					return 4;
				}else {
					return 5;
				}
		 }else if (lvl==5) {
			 if (ed<558) {
					return 3;
				}else if (ed<5621) {
					return 4;
				}else {
					return 5;
				}
		 }
		  return 1; 
	  }
	/**
	 *  震慑
	 */
	  public static int moz(int ed,int lvl){
		  if (lvl==3) {
			   if (ed<426) {
					return 2;
				}else if (ed<3098) {
					return 3;
				}else if (ed<9866){
					return 4;
				}else {
					return 5;
				}
		 }else if (lvl==5) {
			 if (ed<226) {
					return 3;
				}else if (ed<1638) {
					return 4;
				}else if (ed<5215){
					return 5;
				}else if (ed<11868){
					return 6;
				}else {
					return 7;
				}
		 } 
		  return 1;
		}
	/**
	 * 附攻、附防、加速  
	 */
	  public static int moq(int ed,int lvl){
		  if (lvl==3) {
			   if (ed<214) {
					return 2;
				}else if (ed<2155) {
					return 3;
				}else if (ed<8324){
					return 4;
				}else {
					return 5;
				}
		 }else if (lvl==5) {
			 if (ed<117) {
					return 3;
				}else if (ed<1174) {
					return 4;
				}else if (ed<4533){
					return 5;
				}else if (ed<11826){
					return 6;
				}else {
					return 7;
				}
		 } 
		  return 1;
		}
	    /**
		 * 封印、昏睡、毒系 遗忘法术
		 */
		  public static int renq(int ed,int lvl){
			  if (lvl==3) {
				   if (ed<428) {
						return 2;
					}else if (ed<3098) {
						return 3;
					}else if (ed<9866){
						return 4;
					}else {
						return 5;
					}
			 }else if (lvl==5) {
				 if (ed<226) {
						return 3;
					}else if (ed<1638) {
						return 4;
					}else if (ed<5215){
						return 5;
					}else if (ed<11864){
						return 6;
					}else {
						return 7;
					}
			 } 
			  return 1;
			}
		   /**
			 * 混乱法术
			 */
			  public static int renh(int ed,int lvl){
				  if (lvl==3) {
					   if (ed<1362) {
							return 2;
						}else if (ed<9866) {
							return 3;
						}else {
							return 4;
						}
				 }else if (lvl==5) {
					 if (ed<973) {
							return 3;
						}else if (ed<7051) {
							return 4;
						}else {
							return 5;
						}
				 } 
				  return 1;
				}
			  /**
			    * 鬼魅惑
			    */
				public static int guiv(int ed,int lvl){
					if (lvl==3) {
					if (ed<2200) {
						return 2;
					}else if (ed<4600) {
						return 3;
					}else if (ed<9600) {
						return 4;
					}else {
						return 5;
					}
					}else if (lvl==5) {
					if (ed<2200) {
						return 3;
					}else if (ed<4600) {
						return 4;
					}else if (ed<9600) {
						return 5;
					}else if (ed<12000) {
						return 6;
					}else {
						return 7;
					}
					} 
						return 1;
					}
						  
				/**
				  * 鬼三尸
				  */
			public static int guis(int ed,int lvl){
				if (lvl==3) {
					if (ed<5200) {
						return 2;
					}else if (ed<6800) {
						return 3;
					}else {
						return 4;
					}
					}else if (lvl==5) {
					if (ed<2200) {
						return 3;
					}else if (ed<6800) {
						return 4;
					}else {
						return 5;
					}
					} 
						return 1;
					}
	public RoleSkillJframe getRoleSkillJframe() {
		return roleSkillJframe;
	}

	public void setRoleSkillJframe(RoleSkillJframe roleSkillJframe) {
		this.roleSkillJframe = roleSkillJframe;
	}

	public JScrollPane getjScrollPane1() {
		return jScrollPane1;
	}

	public void setjScrollPane1(JScrollPane jScrollPane1) {
		this.jScrollPane1 = jScrollPane1;
	}

	public JScrollPane getjScrollPane2() {
		return jScrollPane2;
	}

	public void setjScrollPane2(JScrollPane jScrollPane2) {
		this.jScrollPane2 = jScrollPane2;
	}

	public JList<String> getListspell() {
		return listspell;
	}

	public void setListspell(JList<String> listspell) {
		this.listspell = listspell;
	}

	public JList<String> getListspellctro() {
		return listspellctro;
	}

	public void setListspellctro(JList<String> listspellctro) {
		this.listspellctro = listspellctro;
	}

	public DefaultListModel<String> getModelspell() {
		return modelspell;
	}

	public void setModelspell(DefaultListModel<String> modelspell) {
		this.modelspell = modelspell;
	}

	public DefaultListModel<String> getModelspellctro() {
		return modelspellctro;
	}

	public void setModelspellctro(DefaultListModel<String> modelspellctro) {
		this.modelspellctro = modelspellctro;
	}

	public StringBuilder getBuilder() {
		return builder;
	}

	public void setBuilder(StringBuilder builder) {
		this.builder = builder;
	}

	public JLabel getSkilljLabel() {
		return skilljLabel;
	}

	public void setSkilljLabel(JLabel skilljLabel) {
		this.skilljLabel = skilljLabel;
	}

	public List<Skill> getSkills() {
		return skills;
	}

	public void setSkills(List<Skill> skills) {
		this.skills = skills;
	}


	
	
}
